package com.zhoukx.nio;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 *
 *   演示  buffer到channel
 */
public class NIOFilerChannerl01 {
    public static void main(String[] args) throws IOException {
        String str = "hello,word";
        //创建一个输出流 -> channel
        FileOutputStream fileOutputStream = new FileOutputStream("d:\\file01.txt");
        // 通过fileOutputStream 获取对象的filechannel
        //  这个channel 的真实类型是FileChannelImpl
        FileChannel channel = fileOutputStream.getChannel();
        // 创建一个缓冲区
        ByteBuffer allocate = ByteBuffer.allocate(1024);

        // 写入到缓冲区
        allocate.put(str.getBytes());
        // 对buffer进行反转  filp
        allocate.flip();

        //将byteBuffer直接写入到filechannel中
        channel.write(allocate);
        fileOutputStream.close();
    }
}
